<?php
/**
 * Created by xuan.
 * Author: xuan
 * Date: 2018/8/3
 * Time: 18:24
 */

namespace app\api\controller\v1;

use app\api\model\Address as AddressModel;
use think\Exception;
use think\Log;
use think\Request;
use think\Session;
use app\api\controller\BaseController;

class Address extends  BaseController
{
    /**
     * 添加收货地址
     */
    public function add()
    {
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			$all['mid']=$this->getLoginId();
			if(empty($all['mid'])){
				return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
			}
			$type= $this->request->param('type',1);
			$all['type'] = $type;
			$all['ifdefault'] = $this->request->param('ifdefault',2);
			$tel = $this->request->param('tel');
			if(empty($tel)){
			    return $this->returnResult(-6,'手机号必须填写');
			}
			$tel = trim($tel);
			$tel = str_replace(' ', '', $tel);
			if(strlen($tel) != 11 || !is_numeric($tel)){
			    return $this->returnResult(-6,'手机号填写错误');
			}
			$all['tel'] = $tel;
			if(empty($all['name'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['tel'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['addr'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}
			
			$data = AddressModel::addOneAddress($all);
			
			$datas = $data->toArray();
			$all['id'] = $datas['id'];
			if($all['ifdefault'] == 1){
				$dataone = AddressModel::selectOneAddressByIdMid($all);
				if($dataone){
					AddressModel::updOneAddressDef($all);
				}
			}
			
			if($data){
				return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>""]);
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"post参数不能为空!","data"=>""]);
			
		}
    }
	
	/**
     * 修改收货地址
     */
    public function update()
    {
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			$all['mid']= $this->getLoginId();
			if(empty($all['mid'])){
				return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
			}
			$all['ifdefault'] = $this->request->param('ifdefault',2);
			
			$tel = $this->request->param('tel');
			if(empty($tel)){
			    return $this->returnResult(-6,'手机号必须填写');
			}
			$tel = trim($tel);
			$tel = str_replace(' ', '', $tel);
			if(strlen($tel) != 11 || !is_numeric($tel)){
			    return $this->returnResult(-6,'手机号填写错误');
			}
			$all['tel'] = $tel;
			
			if(empty($all['name'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['addrvalue'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['addr'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}
			
			if(empty($all['id'])){
			    return $this->add();
			}else{
			    $dataone = AddressModel::selectOneAddressByID($all['id']);
			    if($dataone){
			        
			        if($all['ifdefault'] == 1){
			            $dataone = AddressModel::selectOneAddressByIdMid($all);
			            if($dataone){
			                AddressModel::updOneAddressDef($all);
			            }
			        }
			        
			        $data = AddressModel::updOneAddress($all);
			        if($data){
			            return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>""]);
			        }else{
			            return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			        }
			    }else{
			        return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			    }
			}
			
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"post参数不能为空!","data"=>""]);
			
		}
    }
	
	/**
     * 收货地址列表
     */
    public function lists()
    {
        $this->needLogin();
		$mid=$this->getLoginId();
		$type = $this->request->param('type',1);
		$data = AddressModel::addressListsByMid($mid,$type);
		return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
    }
    
    public function defalt_addr(){
        $mid=$this->getLoginId();
        if(empty($mid)){
            return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
        }
        $type = $this->request->param('type',1);
        $data = AddressModel::defaultAddressByMid($mid,$type);
        if(empty($data)){
            $data = \app\common\model\AddressModel::instance()->where(array("mid"=>$mid,'type'=>$type))->order('id','desc')->find();
        }
        
        return $this->returnResult(1,'',$data);
    }
	
	/**
     * 删除一条收货地址
     */
    public function del($id)
    {
		$mid = $this->getLoginId();
		if(empty($mid)){
			return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
		}
		if(empty($id)){
			return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);exit;
		}
		$dataone = AddressModel::selectOneAddressByID($id);
		if($dataone){
			$data = AddressModel::delOneAddress($id);
			if($data){
				return json(["returnCode"=>1,"returnMsg"=>"success!"]);
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
			}
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
    }
	
	/**
     * 修改默认
     */
    public function updatedef()
    {
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			$type = $this->request->param('type',1);
			
			$all['mid']= $this->getLoginId();
			if(empty($all['mid'])){
				return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
			}
			
			if(empty($all['id'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}
			if(empty($type)){
			    return $this->returnResult(-6,'type参数错误');
			}
			$all['type'] = $type;
			
			$dataone = AddressModel::selectOneAddressByIdMid($all);
			if($dataone){
				$data = AddressModel::updOneAddressDef($all);
				if($data){
					return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>""]);
				}else{
					return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
				}
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
			
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"post参数不能为空!","data"=>""]);
			
		}
    }
	
	/**查询一条记录*/
	public function find($id){
	    $mid = $this->getLoginId();
	    
		if(empty($mid)){
			return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
		}
		$data = AddressModel::selectOneAddressByID($id);
		if($data && $data['mid'] == $mid){
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
	}
	
	/**查询一条记录*/
	public function onedefault(){
	    $all['mid']= $this->getLoginId();
		if(empty($all['mid'])){
			return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
		}
			
		$data = AddressModel::selectOneAddressBydefault($all['mid']);
		return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
	}
	
	
}